{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "414c353b",
   "metadata": {},
   "outputs": [],
   "source": [
    "%use fuel(2.3.1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "19448d21",
   "metadata": {},
   "outputs": [],
   "source": [
    "val envs = java.io.File(\"../../.env\")\n",
    "    .readLines()\n",
    "    .map {\n",
    "        it.split(\"=\")[0] to it.split(\"=\")[1].trim('\"')\n",
    "    }.toMap()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "e86bacc4",
   "metadata": {},
   "outputs": [],
   "source": [
    "val session = envs.get(\"AOC_SESSION\")\n",
    "val year = 2021\n",
    "val day = 20"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "8e8817cb",
   "metadata": {},
   "outputs": [],
   "source": [
    "fun getInput(year: Int, day: Int, session: String): String {\n",
    "    val (_, _, result) = \"https://adventofcode.com/$year/day/$day/input\"\n",
    "    .httpGet()\n",
    "    .header(\"cookie\" to \"session=$session\")\n",
    "    .responseString()\n",
    "        \n",
    "    return result.get().trim()\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "21ec30fb",
   "metadata": {},
   "outputs": [],
   "source": [
    "fun submitAnswer(year: Int, day: Int, session: String, level: Int, answer: String): String {\n",
    "    val (_, _, result) = Fuel\n",
    "    .post(\n",
    "        \"https://adventofcode.com/$year/day/$day/answer\", \n",
    "        parameters = listOf(\"level\" to level, \"answer\" to answer))\n",
    "    .header(\"cookie\" to \"session=$session\")\n",
    "    .responseString()\n",
    "        \n",
    "    return result.get()\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "481efb96",
   "metadata": {},
   "outputs": [],
   "source": [
    "val sample = \"\"\"..#.#..#####.#.#.#.###.##.....###.##.#..###.####..#####..#....#..#..##..###..######.###...####..#..#####..##..#.#####...##.#.#..#.##..#.#......#.###.######.###.####...#.##.##..#..#..#####.....#.#....###..#.##......#.....#..#..#..##..#...##.######.####.####.#.#...#.......#..#.#.#...####.##.#......#..#...##.#.##..#...##.#.##..###.#......#.#.......#.#.#.####.###.##...#.....####.#..#..#.##.#....##..#.####....##...##..#...#......#.#.......#.......##..####..#...#.#.#...##..#.#..###..#####........#..####......#..#\n",
    "\n",
    "#..#.\n",
    "#....\n",
    "##..#\n",
    "..#..\n",
    "..###\"\"\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "0d4b50a0",
   "metadata": {},
   "outputs": [],
   "source": [
    "val input = getInput(year, day, session)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "8cbfe646",
   "metadata": {},
   "outputs": [],
   "source": [
    "fun solve(input: String, times: Int): String {\n",
    "    val lines = input.split(\"\\n\")\n",
    "    val alg = lines[0]\n",
    "    val n = lines.size - 2\n",
    "    val m = lines[2].length\n",
    "    var board = mutableMapOf<Pair<Int, Int>, Char>()\n",
    "    for (i in 0 until n)\n",
    "        for (j in 0 until m) {\n",
    "            board[Pair(i, j)] = lines[i + 2][j]\n",
    "        }\n",
    "        \n",
    "    for (k in 1..times) {\n",
    "        val nxt = mutableMapOf<Pair<Int, Int>, Char>()\n",
    "        \n",
    "        for (i in -times * 2 until n + times * 2)\n",
    "            for (j in -times * 2 until n + times * 2) {\n",
    "                var it = 8\n",
    "                var idx = 0\n",
    "                for (di in -1..1)\n",
    "                    for (dj in -1..1) {\n",
    "                        val ni = i + di\n",
    "                        val nj = j + dj\n",
    "                        if (board[Pair(ni, nj)] == '#')\n",
    "                            idx += 1.shl(it)\n",
    "                        it--\n",
    "                    }\n",
    "                nxt[Pair(i, j)] = alg[idx]\n",
    "            }\n",
    "            \n",
    "        board = nxt\n",
    "    }\n",
    "    \n",
    "    return board.count {\n",
    "        it.key.first >= -times &&\n",
    "        it.key.first < n + times &&\n",
    "        it.key.second >= -times &&\n",
    "        it.key.second < m + times &&\n",
    "        it.value == '#'\n",
    "    }.toString()        \n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "4691035c",
   "metadata": {},
   "outputs": [],
   "source": [
    "fun partOne(input: String): String {\n",
    "    return solve(input, 2)\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "0c2ac59b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "35"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "partOne(sample)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "2b7a70ac",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "5249"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "val partOneAns = partOne(input)\n",
    "partOneAns"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "faeb8c01",
   "metadata": {},
   "outputs": [],
   "source": [
    "submitAnswer(year, day, session, 1, partOneAns)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "bd6be20e",
   "metadata": {},
   "outputs": [],
   "source": [
    "fun partTwo(input: String): String {\n",
    "    return solve(input, 50)\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "3c39987b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "3351"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "partTwo(sample)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "b10c39a1",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "15714"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "val partTwoAns = partTwo(input)\n",
    "partTwoAns"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "539929a8",
   "metadata": {},
   "outputs": [],
   "source": [
    "submitAnswer(year, day, session, 2, partTwoAns)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Kotlin",
   "language": "kotlin",
   "name": "kotlin"
  },
  "language_info": {
   "codemirror_mode": "text/x-kotlin",
   "file_extension": ".kt",
   "mimetype": "text/x-kotlin",
   "name": "kotlin",
   "nbconvert_exporter": "",
   "pygments_lexer": "kotlin",
   "version": "1.6.20-dev-6372"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
